package com.socket.nio.nio.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/**
 * @author Hexudong
 * @date 2025/4/12
 */
@Slf4j
public class TestNettyFutureSync {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();

        Future<Integer> future = eventLoop.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("线程: [{}], 执行计算", Thread.currentThread().getName());
                int sum = 0;
                for (int i = 0; i < 100; i++) {
                    sum += i;
                }
                return sum;
            }
        });
        log.debug("线程: [{}], 等待结果", Thread.currentThread().getName());

        // 方式一： 同步获取结果
        log.debug("线程: [{}], 结果: {}", Thread.currentThread().getName(), future.get());
    }
}
